package com.lty.jeeg.server.modules.tms.dao;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.lty.jeeg.server.modules.tms.model.Task;
import org.apache.ibatis.annotations.*;

import org.springframework.stereotype.Repository;

/**
 * @author : LTY
 * @date : 2019-03-26 10:53:09
 * Description :
 */
@Mapper
@Repository
public interface TaskDao {

    /**
     * 根据ID获取
     *
     * @param id ID
     * @return bean
     */
    @Select("select * from tms_task t where t.id = #{id}")
    Task getById(Long id);

    /**
     * 根据ID获取
     *
     * @param projectId projectId
     * @return bean
     */
    @Select("select * from tms_task t where t.projectId = #{projectId}")
    List<Task> getByProjectId(Long projectId);

    /**
     * 删除
     *
     * @param id ID
     * @return 1/0
     */
    @Delete("delete from tms_task where id = #{id}")
    int delete(Long id);

    /**
     * 删除
     *
     * @param id ID
     * @return 1/0
     */
    @Delete("delete from tms_task where projectId = #{id}")
    int deleteBy(Long id);

    /**
     * 修改
     *
     * @param task ID
     * @return 1/0
     */
    int update(Task task);

    /**
     * 修改任务状态
     *
     * @param id     任务ID
     * @param status 任务状态
     * @return
     */
    @Update("UPDATE tms_task t SET t.status=#{status} WHERE t.id=#{id}")
    @ResultType(int.class)
    int changeStatus(@Param("id") Long id, @Param("status") Integer status);

    /**
     * 修改任务优先级
     *
     * @param id     任务ID
     * @param grade 任务优先级
     * @return
     */
    @Update("UPDATE tms_task t SET t.grade=#{grade} WHERE t.id=#{id}")
    @ResultType(int.class)
    int changeGrade(@Param("id") Long id, @Param("grade") Integer grade);

    /**
     * 修改负责人
     * @param id
     * @param participantId
     * @param participantName
     * @return
     */
    @Update("UPDATE tms_task t SET t.participantId=#{participantId},t.participantName=#{participantName} WHERE t.id=#{id}")
    @ResultType(int.class)
    int changeParticipant(@Param("id") Long id, @Param("participantId") String participantId,  @Param("participantName") String participantName);

    /**
     * 修改执行者
     * @param id
     * @param executorId
     * @param executorName
     * @return
     */
    @Update("UPDATE tms_task t SET t.executorId=#{executorId},t.executorName=#{executorName} WHERE t.id=#{id}")
    @ResultType(int.class)
    int changeExecutor(@Param("id") Long id, @Param("executorId") Long executorId,  @Param("executorName") String executorName);

    /**
     * 修改登记工时
     * @param id
     * @param actualTotalWorkingHours
     * @return
     */
    @Update("UPDATE tms_task t SET t.actualTotalWorkingHours=#{actualTotalWorkingHours} WHERE t.id=#{id}")
    @ResultType(int.class)
    int changeaAtualTotalWorkingHours(@Param("id") Long id, @Param("actualTotalWorkingHours") Integer actualTotalWorkingHours);

    /**
     * 修改预计工时
     * @param id
     * @param estimateWorkingHours
     * @return
     */
    @Update("UPDATE tms_task t SET t.estimateWorkingHours=#{estimateWorkingHours} WHERE t.id=#{id}")
    @ResultType(int.class)
    int changeEstimateWorkingHours(@Param("id") Long id,@Param("estimateWorkingHours") Integer estimateWorkingHours);

    /**
     * 修改截止日期
     * @param id
     * @param deadline
     * @return
     */
    @Update("UPDATE tms_task t SET t.deadline=#{deadline} WHERE t.id=#{id}")
    @ResultType(int.class)
    int changeDeadline(@Param("id") Long id,@Param("deadline") Date deadline);

    /**
     * 修改关联任务
     * @param id
     * @param conjunctionTaskId
     * @return
     */
    @Update("UPDATE tms_task t SET t.conjunctionTaskId=#{conjunctionTaskId},t.conjunctionTaskName=#{conjunctionTaskName} WHERE t.id=#{id}")
    @ResultType(int.class)
    int changeConjunctionTask(@Param("id") Long id,@Param("conjunctionTaskId") String conjunctionTaskId,@Param("conjunctionTaskName") String conjunctionTaskName);

    /**
     * 保存
     *
     * @param task bean
     * @return ID
     */
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into tms_task(name,executorId,executorName, projectId, projectName, participantId, participantName, description, actualTotalWorkingHours, estimateWorkingHours, deadline,screenshotPath, status, grade,  createBy, updateBy, isDeleted) values(#{name}, #{executorId}, #{executorName}, #{projectId}, #{projectName}, #{participantId}, #{participantName}, #{description}, #{actualTotalWorkingHours}, #{estimateWorkingHours},#{deadline},#{screenshotPath}, #{status}, #{grade}, #{createBy}, #{updateBy}, #{isDeleted})")
    Long save(Task task);

    /**
     * 计数
     *
     * @param params 筛选条件
     * @return 总数
     */
    int count(@Param("params") Map<String, Object> params);

    /**
     * 列表
     *
     * @param params 筛选条件
     * @param offset 页码
     * @param limit  分页数目
     * @return beanList
     */
    List<Task> list(@Param("params") Map<String, Object> params, @Param("offset") Integer offset, @Param("limit") Integer limit);

    /**
     * 修改任务置顶
     *
     * @param id     任务ID
     * @param isTop 是否置顶
     * @return
     */
    @Update("UPDATE tms_task t SET t.isTop=#{isTop} WHERE t.id=#{id}")
    @ResultType(int.class)
    int updateIsTop(@Param("id") Long id, @Param("isTop") Integer isTop);

    /**
     * 根据任务ID获取项目
     * @param taskId
     * @return
     */
    @Select("SELECT CONCAT(CASE WHEN p2.name IS NULL THEN '' ELSE CONCAT(p2.name,'-') END, p1.name, '-', t.name) FROM tms_task t  LEFT JOIN tms_project p1 ON t.projectId = p1.id LEFT JOIN tms_project p2 ON p1.parentId = p2.id WHERE t.id=#{taskId}")
    String getProjectNameAndTaskName(@Param("taskId") Long taskId);
}
